<template>
  <div class="container with-nav-padding">
    <is-not-vip :url="imgUrl" v-if="level === 0 && vip_rule === 1"></is-not-vip>
    <is-vip :userInfo="userInfo" v-if="vip_rule === 0"></is-vip>
    <vip-rule :userInfo="userInfo" v-if="level !== 0 && vip_rule === 1" />
    <!-- <div class="item-panel">
      <div class="btn-bar">
        <div
          v-for="(item, index) in tagList"
          :style="{ width: 100 / tagList.length + '%' }"
          :class="{ btn_l: true, active: item.id === currentLevel }"
          @click="currentLevel = item.id"
        >
          <img
            v-if="item.id === currentLevel"
            src="@/assets/vip-assets/图层 1.png"
            alt=""
          />
          <img v-else src="@/assets/vip-assets/礼包.png" alt="" />
          <span>{{ item.vip_price }}元礼包</span>
        </div>
      </div>
      <div class="banner-img"></div>
    </div> -->
    <div class="item-panel_small-img">
      <div class="vip-item">
        <img
          v-if="goodList.length"
          @click="$router.push('/item-detail/' + goodList[0].id)"
          :src="goodList[0].img_show"
          alt=""
        />
      </div>
      <!-- <item-small-img
        v-for="item in goodList"
        :item="item"
      ></item-small-img> -->
    </div>
  </div>
</template>

<script>
import itemSmallImg from "@/components/item-small-img.vue";
import loadMore from "@/components/load-more";
import isVip from "./component/is-vip";
import VipRule from './component/vip-rule';
import isNotVip from "./component/is-not-vip";
import { mapState, mapActions, mapMutations } from "vuex";
import { getVipPage, getVipGood } from "@/api/vip";
export default {
  data() {
    return {
      imgUrl: "",
      tagList: [],
      goodList: [],
      page: 1,
      currentLevel: null,
      isAll: false,
      loading: false,
      userInfo: {}
    };
  },
  watch: {
    currentLevel: function(val) {
      this.page = 1;
      this.isAll = false;
      this.goodList = [];
      this.getVipGood(val, 1);
    }
  },
  computed: {
    ...mapState({
      level: state => state.user.level,
      vip_rule: state => state.app.vip_rule
    })
  },
  methods: {
    ...mapMutations(["setLevel"]),
    getVipGood(id, page) {
      getVipGood({
        page,
        level: 1
      }).then(res => {
        if (res.status === 200) {
          this.goodList.push(...res.data.data.commodity);
          if (res.data.data.commodity.length === 0) {
            this.isAll = true;
          }
        }
        this.loading = false;
      });
    }
  },
  created() {
    getVipPage().then(res => {
      
      if (res.status === 200) {
        this.setLevel(res.data.data.level);
        // if (res.data.data.level === 0) {
        //   this.imgUrl = res.data.data.info;
        // } else {
        //   this.userInfo = res.data.data.info;
        // }
        if (this.vip_rule === 0) {
          this.userInfo = res.data.data.info
        } else {
          if (res.data.data.level === 0) {
            this.imgUrl = res.data.data.info;
          } else {
            this.userInfo = res.data.data.info;
          }
        }
        this.tagList = res.data.data.commodity.level;
        this.goodList = res.data.data.commodity.commodity;
        this.currentLevel = this.tagList[0].id;
      } else if(res.status == 401) {
        console.log(res, '****')
         this.$router.push("/main-login");
      }
    });
  },
  components: {
    itemSmallImg,
    isVip,
    isNotVip,
    loadMore,
    VipRule
  }
};
</script>

<style lang="less" scoped>
.container {
  .item-panel {
    background: #fff;
    margin-top: 0.48rem;
    .btn-bar {
      width: 100%;
      height: 2rem;
      padding: 0 0;
      display: flex;
      line-height: 2rem;
      .btn_l {
        background-color: #f7d5d7;
        color: #666666;
        text-align: center;
        font-size: 0.72rem;
        img {
          height: 0.76rem;
          width: 0.76rem;
          margin-right: 0.3rem;
        }
      }
      .active {
        background-color: #fd555e;
        color: #ffffff;
      }
      .btn_r {
        background-color: #f7d5d7;
        color: #444444;
        text-align: center;
        font-size: 0.72rem;
        img {
          height: 0.76rem;
          width: 0.76rem;
          margin-right: 0.3rem;
        }
      }
    }
  }
  .vip-item {
    margin-top: 0.48rem;
    img {
      width: 100%;
    }
  }
}
</style>
